<template>
  <div class="page">
    <Form :label-width="140">
      <FormItem label="发货类型" required>
        <Select placeholder="选择发货类型" clearable v-model="formData.logisticsType">
          <Option :value="1">快递公司</Option>
          <Option :value="2">同城配送</Option>
          <Option :value="3">虚拟发货</Option>
          <Option :value="4">自提/到店</Option>
        </Select>
      </FormItem>
      <FormItem label="发货人" required>
        <Input v-model="formData.senderName" placeholder="请输入发货人"></Input>
      </FormItem>
      <FormItem label="发货人手机号" required>
        <Input v-model="formData.senderMobile" placeholder="请输入发货人手机号"></Input>
      </FormItem>
      <FormItem label="发货地区选择" required>
        <Input v-model="formData.senderAddress" placeholder="请输入发货地"></Input>
      </FormItem>
      <FormItem v-if="formData.logisticsType === 1 || formData.logisticsType === 2" label="快递公司" required>
        <Select placeholder="选择快递公司" clearable v-model="formData.corpCode">
          <Option v-for="item in seletions" :key="item.id" :value="item.code">{{ item.name }}</Option>
        </Select>
      </FormItem>
      <FormItem v-if="formData.logisticsType === 1 || formData.logisticsType === 2" label="物流运单号">
        <Input v-model="formData.waybillNo" placeholder="请输入物流运单号"></Input>
      </FormItem>
    </Form>
    <div style="text-align: center">
      <Button @click="Submit" type="success">一键发货</Button>
    </div>
  </div>
</template>
<script>
export default {
  name: 'DeliverBatch',
  data() {
    return {
      seletions: this.$dict.logisticsCorp.data,
      orderArray: [],
      orderItem: {},
      formData: {
        logisticsType: 4, //1快递公司，2同城配送，3虚拟商品，4用户自提
        corpCode: null,
        corpName: null,
        senderName: null,
        senderMobile: null,
        senderAddress: null,
        waybillNo: null,
      },
    }
  },
  props: {
    callbank: Function,
  },
  methods: {
    init(orders) {
      this.orderArray = orders
      this.lastedTms()
    },
    lastedTms() {
      new this.$rest({
        gateway: 'agroerp',
        service: 'agroerp-tms',
        url: '/v1.0/plus/shop/tms/order/last',
        query: {
          equal: 9,
        },
        action: (res) => {
          if (res.data.code === 20000) {
            const data = res.data.data
            this.formData.senderName = data.senderName
            this.formData.senderMobile = data.senderMobile
            this.formData.senderAddress = data.senderAddress
          } else {
            this.$Message.error({
              content: res.data.msg,
            })
          }
        },
      }).get()
    },
    //自助发货
    Submit() {
      if (this.formData.logisticsType <= 2 && !this.formData.corpCode) {
        this.$Message.error('请选择快递公司')
        return false
      }
      if (this.formData.logisticsType <= 2 && !this.formData.waybillNo) {
        this.$Message.error('请输入运单号')
        return false
      }
      if (this.formData.logisticsType <= 2) {
        this.formData.corpName = this.$dict.logisticsCorp.name(this.formData.corpCode)
      } else {
        this.formData.corpName = '自提/虚拟发货'
      }

      let tmsOrderForms = []
      for (let i = 0; i < this.orderArray.length; i++) {
        const item = this.orderArray[i]
        let tmsOrderForm = {
          shopId: this.$store.getShop('shopId'),
          orgId: this.$store.getOrg('orgId'),
          cargoName: '商品',
          printType: 1, //00仅下单不打印，1生成图片短链
          shopOrderId: item.shopOrderId,
          recvName: item.consigneeName,
          recvMobile: item.consigneeMobile,
          recvAddress: item.provinceName + item.cityName + item.countyName + item.addressDetail,
          corpCode: this.formData.corpCode,
          corpName: this.formData.corpName,
          senderName: this.formData.senderName,
          senderMobile: this.formData.senderMobile,
          senderAddress: this.formData.senderAddress,
          waybillNo: this.formData.waybillNo,
          logisticsType: this.formData.logisticsType,
        }
        tmsOrderForms.push(tmsOrderForm)
      }

      new this.$rest({
        gateway: 'agroerp',
        service: 'agroerp-tms',
        url: '/v1.0/plus/express/shop/send/self/batch',
        data: {
          pojo: tmsOrderForms,
        },
        action: (res) => {
          if (res.data.code === 20000) {
            this.$Message.success('发货成功')
            this.callbank()
          } else {
            this.$Notice.warning({
              title: '操作失败',
              desc: res.data.msg,
            })
          }
        },
      }).post()
    },
  },
  created() {},
}
</script>
<style lang="less" scoped>
.page {
  width: 100%;
}
.head-box {
  display: flex;
  width: 100%;
  justify-content: space-between;
  min-height: 30px;
}
.head-box .box-item:nth-child(1) {
  width: 30%;
}
.head-box .box-item:nth-child(2) {
  width: 30%;
}
.head-box .box-item:nth-child(3) {
  width: 40%;
}
.btns {
  margin: 16px 0px;
}
.item-image {
  display: flex;
  align-items: flex-start;
}
.express-card {
  display: inline-block;
  width: 24%;
  text-align: center;
  position: relative;
  margin: 5px;
  vertical-align: top;
  padding: 10px;
  min-height: 120px;
}
</style>
